package Models

import (
	"gorm.io/gorm"
	"gorm_study/Table"
	"gorm_study/libs"
)

func UpdateStudent() {
	lists := make([]Table.Student, 0)
	_ = libs.DB.Find(&lists, "age > ?", 20)

	for i := range lists {
		value := lists[i] // 使用副本而不是指针
		age := value.Age + 1
		_ = libs.DB.Model(&value).Update("age", age)
	}
}

func UpdateSuper() {
	result := libs.DB.Model(&Table.Student{}).
		Where("age = ?", 12).
		Updates(Table.Student{Name: "第一个本命年限定"})

	println(result.RowsAffected) // 更新的记录数目
}

func UpdateGlobal() {
	err := libs.DB.Model(&Table.Student{}).Update("tel", "14124124124")
	println(err.Error)
}

func PassYear() {
	result := libs.DB.Model(&Table.Student{}).
		Where("1 = 1").
		Update("age", gorm.Expr("age + ?", 1))

	println(result.RowsAffected) // 更新的记录数目
}
